#!/bin/bash

set -o errexit
#set -o pipefail

export LC_ALL=C

AN_DIR=$1

i=0

for dn in `find "$AN_DIR" -maxdepth 1 -mindepth 1 -type d -name 'ann*' `; do
  
  fn=$dn/e_5/results_gene_ontology.txt.gz

  if [ ! -f $fn ] ; then

    rm -r $dn
    #echo $dn

    i=$(( i + 1 ))
  fi

done

find "$AN_DIR" -type d -name 'ann*' | sort \
    | xargs -n1 -I {} bash -c "echo -ne {}'\t'; cat {}/gene.txt | cut -f1" \
    > $AN_DIR/.tmp_content

j=`join -t$'\t' -v1 \
    <(cat $AN_DIR/.tmp_content | cut -f1) \
    <(cat $AN_DIR/.tmp_content | sort -s -u -k2,2 -t$'\t' | cut -f1 | sort) \
    | wc -l`

join -t$'\t' -v1 \
    <(cat $AN_DIR/.tmp_content | cut -f1) \
    <(cat $AN_DIR/.tmp_content | sort -s -u -k2,2 -t$'\t' | cut -f1 | sort) \
    | xargs -r -n1 rm -r

rm $AN_DIR/.tmp_content

echo "Delete $i bad and $j duplicated annotation subdirs from ${AN_DIR}" >> /dev/stderr

